var TICKS_PER_BEAT = 10;
var noteEvents = [];
var 
    preTime = new Date().getTime() / TICKS_PER_BEAT,
    currentTime = 0;

var recordStart = function () {
    preTime = new Date().getTime() / TICKS_PER_BEAT,
    currentTime = 0;
    noteEvents.splice(0, noteEvents.length - 1);
};


var keyDown = function (note) {
    currentTime = new Date().getTime() / TICKS_PER_BEAT;
    noteEvents.push(MidiEvent.noteOn(note, currentTime - preTime));
    preTime = currentTime;
};

var keyUp = function (note) {
    currentTime = new Date().getTime() / TICKS_PER_BEAT;
    noteEvents.push(MidiEvent.noteOff(note, currentTime - preTime));
    preTime = currentTime;
};

var saveSong = function () {
    var track = new MidiTrack({ events: noteEvents });
    var song = MidiWriter({ tracks: [track] });
    return song.b64;
    //Windows.UI.Popups.MessageDialog(song.b64).showAsync();
    //document.getElementById("content").value = song.b64;
    //song.save();
};